#include "..\Global.h"

map<int8_t, World *> Worlds::worlds;

World * Worlds::GetWorld(int8_t id) {
	return (worlds.find(id) == worlds.end() ? nullptr : worlds[id]);
}

void Worlds::LoadWorld(PacketReader &packet) {
	int8_t id = packet.Read<int8_t>();
	if ((uint8_t)id == 0xFF) return;
	World *world = new World;
	world->id = id;
	world->name = packet.ReadString();
	world->ribbon = packet.Read<int8_t>();
	world->eventmsg = packet.ReadString();
	packet.Skip(4); // [SHORT, EXP RATE] [SHORT, DROPRATE]
	packet.Skip(1); // Unknown lol
	auto channels = packet.Read<int8_t>();
	for (auto i = 0; i < channels; i++) {
		Channel *channel = new Channel;
		channel->name = packet.ReadString(); // Skip string.
		channel->load = packet.Read<int32_t>();
		packet.Skip(1);
		channel->id = packet.Read<int8_t>();
		packet.Skip(1);
		world->channels[i] = channel;
	}
	
	auto messages = packet.Read<int16_t>();
	for (auto i = 0; i < messages; i++) {
		packet.Skip(4); // [SHORT, X] [SHORT, Y]
		packet.ReadString(); // Message
	}
	world->img = (Sprite)wz::top["UI"]["Login"]["WorldSelect"]["BtWorld"][tostring((int)id)]["normal"]["0"];
	worlds[id] = world;
}